﻿Imports System.Data.OleDb
Public Class Form1
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim da As OleDbDataAdapter
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Try
            Dim str As String, i As Integer, j As Integer
            str = "provider=microsoft.jet.oledb.4.0; data source=" & OpenFileDialog1.FileName & ";"
            con = New OleDbConnection(str)
            con.Open()
            Dim dt As System.Data.DataTable
            dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim d As DataRow
            For Each d In dt.Rows
                If UCase(d("table_type").ToString) = "TABLE" Then
                    TreeView1.Nodes.Add(d("Table_name").ToString)
                    str = "select * from " & d("Table_name").ToString
                    Dim ds As DataSet
                    Dim ad As OleDbDataAdapter
                    ad = New OleDbDataAdapter(str, con)
                    ds = New DataSet
                    ad.Fill(ds, "stock")
                    Dim k As Integer
                    For i = 0 To ds.Tables(0).Columns.Count - 1
                        TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                        TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                    Next
                    ds.Clear()
                    j = j + 1
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub AddDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddDatabaseToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim i As Integer, j As Integer
        Dim c As Integer
        Dim txtquery As String
        While i < TreeView1.Nodes.Count
            If TreeView1.Nodes(i).Checked = True Then
                txtquery = txtquery & " SELECT "
                c = 0
                While j < TreeView1.Nodes(i).Nodes.Count
                    If TreeView1.Nodes(i).Nodes(j).Checked = True Then
                        txtquery = txtquery & " " & TreeView1.Nodes(i).Nodes(j).Text & ","
                        c = c + 1
                    End If
                    j = j + 1
                End While
                If c = 0 Then
                    txtquery = txtquery & " * "
                Else
                    txtquery = txtquery.Substring(0, txtquery.Length - 1)
                End If
                txtquery = txtquery & " FROM " & TreeView1.Nodes(i).Text & vbCrLf
                'Query_and_Report1.enterquery(txtquery)
                j = 0
            End If
            i = i + 1
        End While
    End Sub
End Class
